package com.tanhua.server.controller;

import com.tanhua.model.db.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.intercepror.UserHolder;
import com.tanhua.server.service.UsersService;
import javafx.geometry.Pos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UsersService usersService;

    /**
     * 查看用户信息
     * GET/users
     * Headers Authorization
     * Query：userID  用户id，当不传递时，查询当前用户的资料信息
     * 返回值：    userInfo对象
     */
    @GetMapping
    public ResponseEntity users(Long userID) {
        //处理请求
        if (userID == null) {
            userID = UserHolder.getUserId();//转型
        }
        //调用service
        UserInfoVo info = usersService.findUserInfoId(userID);
        //构造返回
        return ResponseEntity.ok(info);
    }

    /**
     * 更新用户信息
     * PUT/users
     * Authorization
     * Body:  UserInfo
     * 返回值   无
     */
    @PutMapping
    public ResponseEntity updataUserInfo(@RequestBody UserInfo userInfo) {

        Long userId = UserHolder.getUserId();//转型
        userInfo.setId(userId);
        //调用service
        usersService.updataUserInfo(userInfo);

        //构建返回值
        return ResponseEntity.ok(null);
    }

    /**
     * 查询通用设置
     * GET   /settings
     * 返回值：    SettingsVo
     */
    @GetMapping("/settings")
    public ResponseEntity settings() {
        SettingsVo vo = usersService.settings();
        return ResponseEntity.ok(vo);
    }

    /**
     * 设置陌生人问题
     * POST /questions
     * 请求参数：
     * Body：  content
     * 不需要返回值
     */

    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map) {
        //解析请求参数
        String content = map.get("content").toString();
        usersService.questions(content);
        return ResponseEntity.ok(null);
    }

    /**
     * 设置通知开关
     * 路径  POST     notifications/setting
     * Authorization
     * Body:
     * likeNotification	boolean
     * pinglunNotification	boolean类型
     * gonggaoNotification
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity setting(@RequestBody Map map) {
        //1.解析请求参数
        String like = map.get("likeNotification").toString();
        String pinglun = map.get("pinglunNotification").toString();
        String gonggao = map.get("gonggaoNotification").toString();
        Boolean likeNotifi = Boolean.valueOf(like);
        Boolean pinglunNotifi = Boolean.valueOf(pinglun);
        Boolean gonggaoNotifi = Boolean.valueOf(gonggao);
        //2.调用service
        usersService.setting(likeNotifi, pinglunNotifi, gonggaoNotifi);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询黑名单分页列表
     * GET         /users/blacklist?page=1&pagesize=5
     * 请求参数：
     * 1.Authorization   2.Query:方式的直接写，分页给个默认值  page   pagesize
     * 返回数据：
     * PageResult
     */
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        //调用service，返回值PageResult
        PageResult pageResult = usersService.blacklist(page, pagesize);
        //构建返回值
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 移除黑名单
     * DELETE      /users/blacklist/{uid}
     * 请求参数：
     * uid：路径参数
     * /blacklist/6
     */
    @DeleteMapping("/blacklist/{blackUserId}")
    public ResponseEntity blacklist(@PathVariable("blackUserId") Long blackUserId) {
        //调用service
        usersService.blackUserId(blackUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询互相喜欢、喜欢、粉丝谁看过我
     * GET    /friends/:type
     * type：
     * 1 互相关注
     * 2 我关注
     * 3 粉丝
     * 4 谁看过我    page  pagesize
     * 返回值： PageResult
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity friends(@PathVariable("type") Integer type,
                                  @RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult vo = usersService.friends(type, page, pagesize);
        return ResponseEntity.ok(vo);
    }

    /**
     * 修改手机号- 1 发送短信验证码
     * POST  /phone/sendVerificationCode
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {

        usersService.sendVerificationCode();
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号 - 2 校验验证码
     * POST  /phone/checkVerificationCode
     * 请求参数：Body
     * verificationCode:验证码
     * 返回
     * verification：是否通过验证
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map) {
        String code = map.get("verificationCode").toString();
        Boolean verification1 = usersService.checkVerificationCode(code);
        Map<String, Boolean> verification = new HashMap<>();
        verification.put("verification",verification1);
        return ResponseEntity.ok(verification);
    }

    /**
     * 修改手机号 - 3 保存
     * POST  /phone
     * 请求：Body：
     * phone：手机号
     * 返回空
     */
    @PostMapping("/phone")
    public ResponseEntity phone(@RequestBody Map map) {
        String phone = map.get("phone").toString();
        usersService.phone(phone);
        return ResponseEntity.ok(null);
    }

}
